package com.xiaoyu.base.push;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.xiaoyu.base.log.BaseLogClient;

import in.srain.cube.app.init.InitializableAsync;
import in.srain.cube.app.init.InitializableSync;
import in.srain.cube.util.UTUtil;
import in.srain.cube.util.log.LogEvent;

import in.srain.cube.util.CLog;

public class BasePushAgent implements InitializableSync, InitializableAsync {

    protected static final String TAG = "lib-push";
    private final String mPushType;
    private Context mContext;
    private IPushEventListener mPushEventListener;
    private int mRetryTimes = 0;
    private boolean mIsBusy = false;

    protected BasePushAgent(String pushType) {
        mPushType = pushType;
    }

    public void onCreate(Activity activity) {

    }

    public void onResume(Activity activity) {
    }

    public void onStop(Activity activity) {
    }

    public void onDestroy(Activity activity) {
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    }

    public void setPushEventListener(IPushEventListener listener) {
        mPushEventListener = listener;
    }

    public void onRegisterSuccess(String pushToken) {
        mIsBusy = false;
        CLog.d(TAG, "onRegisterSuccess: %s %s", mPushType, pushToken);
        if (mPushEventListener != null) {
            mPushEventListener.onRegisterSuccess(mPushType, pushToken);
            if (pushToken.equals("unknown")) {
                addGetTokenEventLog("register_default_service", "not_support");
            } else {
                addGetTokenEventLog("register_default_service", "success");
            }
        }
    }

    public void onRegisterAlternateSuccess(String alternatePushType, String alternatePushToken) {
        CLog.d(TAG, "onRegisterAlternateSuccess: %s %s", alternatePushType, alternatePushToken);
        if (mPushEventListener != null) {
            mPushEventListener.onRegisterAlternateSuccess(alternatePushType, alternatePushToken);
            addGetTokenEventLog("register_alternate_service", "success");
        }
    }

    public void onRegisterFail(String errorMsg) {
        CLog.d(TAG, "onRegisterFail: %s %s", mPushType, errorMsg);
        if (mPushEventListener != null) {
            mPushEventListener.onRegisterFail(mPushType, errorMsg);
        }
    }

    public void onPassThroughMessage(String msg) {
        CLog.d(TAG, "onPassThroughMessage: %s %s", mPushType, msg);
        if (TextUtils.isEmpty(msg)) {
            return;
        }
        if (mPushEventListener != null) {
            mPushEventListener.onPassThroughMessage(mPushType, msg);
        }
    }

    public void onNotificationClickData(String data) {
        CLog.d(TAG, "onNotificationClickData: %s %s", mPushType, data);
        if (mPushEventListener != null) {
//            mPushEventListener.onPassThroughMessage(mPushType, data);
            mPushEventListener.onNotificationClickData(mPushType, data);
        }
    }

    @Override
    public void initiateSync(Context context) {
        initPushAgent(context);
    }

    private void initPushAgent(Context context) {
        synchronized (this) {
            if (mIsBusy) {
                return;
            }
            mIsBusy = true;
            CLog.i(TAG, "init push agent, type: %s", mPushType);
            onInitPushAgent(context);
            mIsBusy = false;
        }
    }

    protected void onInitPushAgent(Context context) {
    }

    @Override
    public void initiateAsync(Context context) {
        startPushService(context);
    }

    private void startPushService(Context context) {
        mContext = context;
        synchronized (this) {
            if (mIsBusy) {
                return;
            }
            mIsBusy = true;
            CLog.i(TAG, "starting push registration: %s", mPushType);
            tryToStartPushService(context);
        }
    }

    protected void tryToStartPushService(Context context) {
        addGetTokenEventLog("start_register_service", context.toString());
    }

    public synchronized void retryOnFail(String errorMsg) {
        CLog.e(TAG, "register fail: pushType = %s, errorMsg = %s, retryTimes = %s",
                mPushType, errorMsg, mRetryTimes);
        if (mIsBusy) {
            mIsBusy = false;
            mRetryTimes++;
            addGetTokenEventLog("retry_on_fail", "get_default_service_fail_and_code_is_" + errorMsg);
            if (mRetryTimes == 3) {
                CLog.e(TAG, "notify onRegisterFail: pushType = %s, errorMsg = %s, retryTimes = %s",
                        mPushType, errorMsg, mRetryTimes);
                if (mPushEventListener != null) {
                    mPushEventListener.onRegisterFail(mPushType, errorMsg);
                }
            } else if (mRetryTimes < 3) {

                CLog.e(TAG, "tryToStartPushService: pushType = %s, errorMsg = %s, retryTimes = %s",
                        mPushType, errorMsg, mRetryTimes);

                mIsBusy = true;
                tryToStartPushService(mContext);
            }
        }
    }

    protected void addGetTokenEventLog(String when, String des) {
        LogEvent logEvent = new LogEvent("sync-push-token");
        logEvent.putData("when", when);
        logEvent.putData("pushType", getPushType());
        logEvent.putData("des", des);
        BaseLogClient.Companion.getInstance().addLog(logEvent);
    }

    public String getPushType() {
        return mPushType;
    }
}